<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式系统最终一致性实现方案</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border: 1px solid rgba(102, 126, 234, 0.3);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .advantage-tag {
            background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%);
            color: #1a202c;
            padding: 2px 12px;
            border-radius: 20px;
            font-size: 0.875rem;
            font-weight: 500;
            display: inline-block;
            margin-right: 8px;
            margin-bottom: 8px;
        }
        
        .disadvantage-tag {
            background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
            color: #1a202c;
            padding: 2px 12px;
            border-radius: 20px;
            font-size: 0.875rem;
            font-weight: 500;
            display: inline-block;
            margin-right: 8px;
            margin-bottom: 8px;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .number-circle {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 1.25rem;
            flex-shrink: 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .fade-in-up {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center fade-in-up">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">分布式系统最终一致性</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                在分布式架构中实现数据一致性的九大核心方案
            </p>
            <div class="mt-10 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-network-wired text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">分布式架构</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-sync-alt text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">数据同步</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-check-double text-4xl mb-2"></i>
                    <p class="text-sm opacity-80">最终一致</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 fade-in-up">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>分布式系统中，实现最终一致性是保证数据在多个节点或服务中达到一致性的关键目标。最终一致性要求在系统稳定且无新的写入操作的情况下，所有的数据副本将最终达到一致的状态。
            </p>
        </div>

        <!-- Visualization -->
        <div class="mb-16 fade-in-up">
            <h2 class="text-3xl font-bold text-center mb-8 text-gray-800">最终一致性实现方案全景图</h2>
            <div class="mermaid">
                graph TB
                    A[最终一致性] --> B[数据复制策略]
                    A --> C[事务处理机制]
                    A --> D[协调与同步]
                    A --> E[冲突处理]
                    
                    B --> B1[异步复制]
                    B --> B2[数据版本化]
                    B --> B3[一致性哈希]
                    
                    C --> C1[补偿事务SAGA]
                    C --> C2[分布式锁]
                    
                    D --> D1[Gossip协议]
                    D --> D2[读写一致性设置]
                    
                    E --> E1[最后写入胜]
                    E --> E2[合并策略]
                    E --> E3[应用级解决]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#84fab0,stroke:#fff,stroke-width:2px
                    style C fill:#8fd3f4,stroke:#fff,stroke-width:2px
                    style D fill:#fee140,stroke:#fff,stroke-width:2px
                    style E fill:#fa709a,stroke:#fff,stroke-width:2px
            </div>
        </div>

        <!-- Solutions Grid -->
        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            <!-- 1. 异步复制 -->
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover fade-in-up">
                <div class="flex items-center mb-6">
                    <div class="number-circle">1</div>
                    <h3 class="text-2xl font-bold ml-4 text-gray-800">异步复制</h3>
                </div>
                <p class="text-gray-600 mb-6 leading-relaxed">
                    在异步复制中，数据在主节点写入后，立即返回成功，然后后台进行数据复制到从节点。这样，系统的写操作不会等待所有副本的确认，能够提高写入性能。
                </p>
                <div class="mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2"><i class="fas fa-plus-circle text-green-500 mr-2"></i>优点</h4>
                    <div>
                        <span class="advantage-tag">高效写入性能</span>
                        <span class="advantage-tag">低延迟响应</span>
                    </div>
                </div>
                <div class="mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2"><i class="fas fa-minus-circle text-red-500 mr-2"></i>缺点</h4>
                    <div>
                        <span class="disadvantage-tag">短时数据不一致</span>
                        <span class="disadvantage-tag">需要故障恢复机制</span>
                    </div>
                </div>
                <div class="mt-6 p-4 bg-purple-50 rounded-lg">
                    <p class="text-sm text-purple-700"><i class="fas fa-lightbulb mr-2"></i><strong>应用场景：</strong>社交媒体平台的用户数据同步</p>
                </div>
            </div>

            <!-- 2. 数据版本化 -->
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover fade-in-up">
                <div class="flex items-center mb-6">
                    <div class="number-circle">2</div>
                    <h3 class="text-2xl font-bold ml-4 text-gray-800">数据版本化</h3>
                </div>
                <p class="text-gray-600 mb-6 leading-relaxed">
                    数据版本化通过为每个数据项分配一个版本号，以标识数据的变化。系统可以根据版本号来决定数据的最新状态。
                </p>
                <div class="mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2"><i class="fas fa-plus-circle text-green-500 mr-2"></i>优点</h4>
                    <div>
                        <span class="advantage-tag">冲突检测</span>
                        <span class="advantage-tag">历史可追踪</span>
                    </div>
                </div>
                <div class="mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2"><i class="fas fa-minus-circle text-red-500 mr-2"></i>缺点</h4>
                    <div>
                        <span class="disadvantage-tag">版本管理复杂</span>
                    </div>
                </div>
                <div class="mt-6 p-4 bg-purple-50 rounded-lg">
                    <p class="text-sm text-purple-700"><i class="fas fa-lightbulb mr-2"></i><strong>应用场景：</strong>Cassandra、DynamoDB等分布式数据库</p>
                </div>
            </div>

            <!-- 3. 最终一致性模型 -->
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover fade-in-up">
                <div class="flex items-center mb-6">
                    <div class="number-circle">3</div>
                    <h3 class="text-2xl font-bold ml-4 text-gray-800">最终一致性模型</h3>
                </div>
                <p class="text-gray-600 mb-6 leading-relaxed">
                    最终一致性模型允许系统在短时间内存在数据不一致，但要求在没有新的写入操作的情况下，所有副本最终达到一致状态。
                </p>
                <div class="mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2"><i class="fas fa-plus-circle text-green-500 mr-2"></i>优点</h4>
                    <div>
                        <span class="advantage-tag">高吞吐量</span>
                        <span class="advantage-tag">容错能力强</span>
                    </div>
                </div>
                <div class="mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2"><i class="fas fa-minus-circle text-red-500 mr-2"></i>缺点</h4>
                    <div>
                        <span class="disadvantage-tag">一致性延迟</span>
                        <span class="disadvantage-tag">需要监控管理</span>
                    </div>
                </div>
                <div class="mt-6 p-4 bg-purple-50 rounded-lg">
                    <p class="text-sm text-purple-700"><i class="fas fa-lightbulb mr-2"></i><strong>应用场景：</strong>Amazon S3、Google Cloud Storage</p>
                </div>
            </div>

            <!-- 4. 补偿事务（SAGA） -->
            <div class="bg-white rounded-xl shadow-lg p-8 card-hover fade-in-up">
                <div class="flex items-center mb-6">
                    <div class="number-circle">4</div>
                    <h3 class="text-2xl font-bold ml-4 text-gray-800">补偿事务（SAGA）</h3>
                </div>
                <p class="text-gray-600 mb-6 leading-relaxed">
                    SAGA将一个长事务拆分为多个短事务，每个短事务都有一个补偿操作。如果某个短事务失败，则执行补偿操作来撤销之前成功的短事务。
                </p>
                <div class="mb-4">
                    <h4 class="font-semibold text-gray-700 mb-2"><i class="fas fa-plus-circle text-green-500 mr-